package org.bitterorange.flansmodslain.interaction;

import org.bitterorange.flansmodslain.actions.Point;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class PointsCommand implements CommandExecutor {
    private static void trySendHelpMessageTo(CommandSender sender) {
        if (!sender.hasPermission("flansmodslain.points.help")) {
            sender.sendMessage("§c你没有权限获取这个命令的帮助。");
            return;
        }
        sender.sendMessage("§c命令格式错误。正确格式：");
        sender.sendMessage("§c/points <show> <玩家ID>");
        sender.sendMessage("§c/points <add/set> <玩家ID> <点数>");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
        if (args.length == 3) {
            String cmd = args[0];
            if (cmd.equalsIgnoreCase("add")) {
                if (!sender.hasPermission("flansmodslain.points.add")) {
                    sender.sendMessage("§c你没有权限执行这个命令。");
                    return true;
                }
                Player player = Bukkit.getPlayer(args[1]);
                if (player == null) {
                    sender.sendMessage("§c命令执行失败：目标玩家不存在。");
                    return true;
                }
                int points;
                try {
                    points = Integer.parseInt(args[2]);
                } catch (NumberFormatException e) {
                    sender.sendMessage("§c命令执行失败：增加的点数应该是一个整数。");
                    return true;
                }
                Point.add(player, points);
                sender.sendMessage(String.format("§6成功地将玩家§c%s§6的点数设置为§c%d。", player.getDisplayName(), Point.get(player)));
            } else if (cmd.equalsIgnoreCase("set")) {
                if (!sender.hasPermission("flansmodslain.points.set")) {
                    sender.sendMessage("§c你没有权限执行这个命令。");
                    return true;
                }
                Player player = Bukkit.getPlayer(args[1]);
                if (player == null) {
                    sender.sendMessage("§c命令执行失败：目标玩家不存在。");
                    return true;
                }
                int points;
                try {
                    points = Integer.parseInt(args[2]);
                } catch (NumberFormatException e) {
                    sender.sendMessage("§c命令执行失败：设置的的点数应该是一个整数。");
                    return true;
                }
                Point.set(player, points);
                sender.sendMessage(String.format("§6成功地将玩家§c%s§6的点数设置为§c%d§6。", player.getDisplayName(), Point.get(player)));
            } else {
                trySendHelpMessageTo(sender);
            }
        } else if (args.length == 2) {
            if (!args[0].equalsIgnoreCase("show")) {
                trySendHelpMessageTo(sender);
                return true;
            }
            if (!sender.hasPermission("flansmodslain.points.show")) {
                sender.sendMessage("§c你没有权限执行这个命令。");
                return true;
            }
            Player player = Bukkit.getPlayer(args[1]);
            if (player == null) {
                sender.sendMessage("§c命令执行失败：目标玩家不存在。");
                return true;
            }
            sender.sendMessage(String.format("§6玩家§c%s§6的点数为§c%d§6。", player.getDisplayName(), Point.get(player)));
        } else {
            trySendHelpMessageTo(sender);
        }
        return true;
    }
}